/* ******************************************************
BreadboArduino_Hello_World Joe Pardue February 10, 2010
****************************************************** */
#include <stdio.h>
#include <avr/io.h>

#define FOSC 16000000
#define BAUD 57600
#define MYUBRR FOSC/16/BAUD-1

uint8_t receiveByte( void );
void init();

// From example in avrlibc manual
static int uart_putchar(char c, FILE *stream);
static FILE mystdout = \
FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

int main(void) 
{ 
    uint8_t b;
    uint8_t count = 0;

    init();

    printf("BBArduino_Hello_Test v 005\n");

    while(1)
    {
		b = receiveByte();

		printf("#%d",count++); 
		printf("You sent: %c\n",b); 
    }

    return 0;
}

static int uart_putchar(char c, FILE *stream)
{ 
    if (c == '\n') uart_putchar
    ('\r', stream);
    // wait for UDR to be clear
    loop_until_bit_is_set(UCSR0A, UDRE0); 
    UDR0 = c;    //send the character
    return 0; 
} 

void init()
{
    //USART Baud rate: 57600
    UBRR0H = (MYUBRR) >> 8;
    UBRR0L = MYUBRR;
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);    

    //set the output stream
    stdout = &mystdout;    
}

uint8_t receiveByte( void )
{
    // Wait for data to be received 
    while ( !(UCSR0A & (1<<RXC0)) );	
    // Get and return received data from buffer 
    return UDR0;
}
